package cn.edgar.org.controller;

import cn.edgar.basic.utils.AjaxResult;
import cn.edgar.basic.utils.PageList;
import cn.edgar.org.domain.Department;
import cn.edgar.org.query.DepartmentQuery;
import cn.edgar.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
/**
* @Description: TODO
* @Author: Mr.She
* @Version: 1.0
* @Date:  2022/6/24 11:54
*/
public class DepartmentController1 {

  @Autowired
    private IDepartmentService departmentService;

  @GetMapping("/{id}")
    public Department loadById(@PathVariable("id") Long id){
      return departmentService.loadById(id);
  }


  @GetMapping
    public List<Department> loadAll(){
      return departmentService.loadAll();
  }

  @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
      try {
          departmentService.remove(id);
          return AjaxResult.me();
      }catch (Exception e){
          e.printStackTrace();
          return AjaxResult.me().setSuccess(false).setMessage("系统忙稍后再试！！");
      }
  }

  @PostMapping
    public AjaxResult addOrUpt(@RequestBody Department department){
      try {
          if (department.getId()== null){
              departmentService.save(department);
          }else {
              departmentService.update(department);
          }
          return AjaxResult.me();
      }catch (Exception e){
          e.printStackTrace();
          return AjaxResult.me().setSuccess(false).setMessage("系统繁忙稍后再试！！！！");
      }
  }

  @PostMapping
    public PageList<Department> queryData(@RequestBody DepartmentQuery departmentQuery){
      PageList<Department> data = departmentService.queryData(departmentQuery);
      return data;

  }
}
